package sc.tool.sqlgen.branchinfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.CollectionUtils;

/**
 *
 * <p>比较的结果</p>
 * @author Zhang Yun
 * @version $Id: CompareResult.java, v 0.1 2012-11-3 下午11:00:29 User Exp $
 */
public class CompareResult {

    private int bankDataCount;
    private int dbDataCount;

    private Map<String, DiffUnit> diffUnitBranchIdMap = new HashMap<String, DiffUnit>();

    public int getBankDataCount() {
        return bankDataCount;
    }

    public void setBankDataCount(int bankDataCount) {
        this.bankDataCount = bankDataCount;
    }

    public int getDbDataCount() {
        return dbDataCount;
    }

    public void setDbDataCount(int dbDataCount) {
        this.dbDataCount = dbDataCount;
    }

    private List<DiffUnit> diffUnitList = new ArrayList<DiffUnit>();

    public void addDiffUnit(DiffUnit diffUnit) {
        if (diffUnitBranchIdMap.get(diffUnit.getBankData().getBranchId()) == null) {
            diffUnitList.add(diffUnit);
            diffUnitBranchIdMap.put(diffUnit.getBankData().getBranchId(), diffUnit);
        }
    }

    public int getDiffCount() {
        return this.diffUnitList.size();
    }

    public List<DiffUnit> getDiffUnitList() {
        return diffUnitList;
    }

    public void setDiffUnitList(List<DiffUnit> diffUnitList) {
        this.diffUnitList = diffUnitList;
    }

    public String getDiffDesc() {
        if (CollectionUtils.isEmpty(diffUnitList)) {
            return "没有差异";
        }
        StringBuilder newDesc = new StringBuilder();
        StringBuilder updateDesc = new StringBuilder();

        int newCount = 0;
        int updateCount = 0;
        for (DiffUnit diffUnit : diffUnitList) {
            switch (diffUnit.getDiffType()) {
            case NEW:
                newCount++;
                newDesc.append(diffUnit.getDiffDesc()).append("\n");
                break;
            case UPDATE:
                updateCount++;
                updateDesc.append(diffUnit.getDiffDesc()).append("\n");
                break;
            }
        }

        newDesc.insert(0, "--------------新增[" + newCount + "]条,明细:------------------\n");
        updateDesc.insert(0, "--------------更新[" + updateCount + "]条,明细:--------------\n");
        return newDesc.toString() + updateDesc.toString();
    }

    @Override
    public String toString() {
        return "共比对,DB[" + dbDataCount + "],Bank[" + bankDataCount + "]条数据，其中差异[" + diffUnitList.size() + "]条，明细:\n"
                + getDiffDesc();

    }

}
